home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: 500 MB Amiga Software / 500 MB Amiga Software - Euber 130 - Amiga Games Disc & Mag.iso / userbox / publicdomain / datatypes / multiprint / source / multiprint.c < prev    next >
C/C++ Source or Header  |  1995-08-08  |  7KB  |  301 lines

  1. /*
  2. ** $PROJECT: MultiPrint - print datatype objects
  3. **
  4. ** $VER: MultiPrint.c 39.0 (07.08.95)
  5. **
  6. ** by
  7. **
  8. ** Stefan Ruppert , Windthorststraße 5 , 65439 Flörsheim , GERMANY
  9. **
  10. ** (C) Copyright 1995
  11. ** All Rights Reserved !
  12. **
  13. ** $HISTORY:
  14. **
  15. ** 07.08.95 : 039.000 : initial beta version
  16. **
  17. */
  18.  
  19. /*FS*/ /* $STARTDEFINE: "BumpRev defines"*/
  20. #define VERSION  39
  21. #define REVISION 0
  22. #define DATE "7.8.95"
  23. #define VERS "MultiPrint 39.0beta"
  24. #define VSTRING "MultiPrint 39.0beta (7.8.95)\r\n"
  25. #define VERSTAG "\0$VER: MultiPrint 39.0beta (7.8.95)"
  26. /*FE*/ /* $ENDDEFINE:   */
  27.  
  28. /* ----------------------------- definitions ------------------------------ */
  29.  
  30. /*FS*/ /*"Definitions"*/
  31.  
  32. /* ------------------------------- includes ------------------------------- */
  33.  
  34. #define CATCOMP_BLOCK
  35.  
  36. #include "MultiPrint.h"
  37.  
  38. /* ------------------------------ prototypes ------------------------------ */
  39.  
  40. extern int MultiPrint(void);
  41.  
  42. static void SetError(struct GlobalData *gd,LONG err,STRPTR errobj);
  43. static void NotifyUser(struct GlobalData *gd,LONG msgid,...);
  44. static STRPTR GetString(struct GlobalData *gd,LONG id);
  45. static BOOL FindMethod(Object *obj,ULONG method);
  46.  
  47. /* ------------------------------- strings -------------------------------- */
  48.  
  49. static const STRPTR multiprint       = "MultiPrint";
  50. static const STRPTR version          = VERSTAG;
  51.  
  52. static const STRPTR datatypeslibrary = "datatypes.library";
  53. static const STRPTR printerdevice    = "printer.device";
  54.  
  55. /*FE*/
  56.  
  57. /* ----------------------------- main program ----------------------------- */
  58.  
  59. /*FS*/ GetA4 int MultiPrint(void)
  60. {
  61.     LONG retval = RETURN_FAIL;
  62.     struct GlobalData *gd;
  63.  
  64.     if((gd = Init()) != NULL)
  65.     {
  66.         struct Process *proc = (struct Process *) FindTask(NULL);
  67.         struct RDArgs *args;
  68.         STRPTR errobj = multiprint;
  69.  
  70.         if(proc->pr_CLI == NULL)
  71.         {
  72.             /* workbench start not supported */
  73.             struct Message *msg;
  74.             WaitPort(&proc->pr_MsgPort);
  75.             msg = GetMsg(&proc->pr_MsgPort);
  76.             Forbid();
  77.             ReplyMsg(msg);
  78.             retval = RETURN_ERROR;
  79.         } else if(!(DOSBase = OpenLibrary("dos.library",39)))
  80.         {
  81.             retval = RETURN_FAIL;
  82.         } else
  83.         {
  84.             retval = RETURN_OK;
  85.  
  86.             if(!(DataTypesBase = OpenLibrary(datatypeslibrary,39)))
  87.                 SetError(gd,ERROR_OBJECT_NOT_FOUND,datatypeslibrary);
  88.             else
  89.             {
  90.                 IntuitionBase = OpenLibrary("intuition.library",39);
  91.                 UtilityBase   = OpenLibrary("utility.library",39);
  92.                 IconBase      = OpenLibrary("icon.library",39);
  93.                 if((LocaleBase    = OpenLibrary("locale.library",38)))
  94.                     gd->gd_Catalog = OpenCatalog(NULL,"multiprint.catalog",
  95.                                                           OC_BuiltInLanguage,"english",
  96.                                                           TAG_DONE);
  97.  
  98.                 if((args = ReadArgs(TEMPLATE,(LONG *) &gd->gd_Para,NULL)) != NULL)
  99.                 {
  100.                     STRPTR *files = gd->gd_Para.td_Files;
  101.                     Object *obj;
  102.  
  103.                     /* printing initialization */
  104.                     if(!(gd->gd_PrintPort = CreateMsgPort()))
  105.                         SetError(gd,ERROR_NO_FREE_STORE,multiprint);
  106.                     else if(!(gd->gd_PrintIO = CreateIORequest(gd->gd_PrintPort,sizeof(union printerIO))))
  107.                         SetError(gd,ERROR_NO_FREE_STORE,multiprint);
  108.                     else if(!(OpenDevice(printerdevice,0,(struct IORequest *) gd->gd_PrintIO,0) == 0))
  109.                         SetError(gd,ERROR_OBJECT_NOT_FOUND,printerdevice);
  110.                     else
  111.                     {
  112.                         /* simulate a GadgetInfo structure */
  113.                         if((gd->gd_GInfo.gi_Screen = LockPubScreen(NULL)))
  114.                         {
  115.                             gd->gd_GInfo.gi_DrInfo = GetScreenDrawInfo(gd->gd_GInfo.gi_Screen);
  116.                         }
  117.  
  118.                         gd->gd_PrintMsg.MethodID     = DTM_PRINT;
  119.                         gd->gd_PrintMsg.dtp_GInfo    = &gd->gd_GInfo;
  120.                         gd->gd_PrintMsg.dtp_PIO      = gd->gd_PrintIO;
  121.                         gd->gd_PrintMsg.dtp_AttrList = NULL;
  122.  
  123.                         while(*files)
  124.                         {
  125.                             /* open the object */
  126.                             if((obj = NewDTObjectA(*files,NULL)))
  127.                             {
  128.                                 struct DataType *dt;
  129.  
  130.                                 /* determine the datatype */
  131.                                 if(GetAttr(DTA_DataType,obj,(ULONG *) &dt) == 1)
  132.                                 {
  133.                                     /* set here datatype specific printer prefs, so that we can print
  134.                                      * c source for example with 136 chars per line and normal text with
  135.                                      * 80 chars per line .
  136.                                      */
  137.                                 }
  138.  
  139.                                 /* supports the object printing ? */
  140.                                 if(FindMethod(obj,DTM_PRINT))
  141.                                 {
  142.                                     LONG rc;
  143.                                     ULONG proc;
  144.  
  145.                                     /* layout the object */
  146.                                     DoMethod(obj,GM_LAYOUT,&gd->gd_GInfo,1);
  147.  
  148.                                     /* just wait until the layout process has finished */
  149.                                     while(GetDTAttrs(obj,DTA_LayoutProc,&proc,TAG_DONE) == 1 && proc)
  150.                                         Delay(25);
  151.  
  152.                                     NotifyUser(gd,MSG_PRINTING_FILE,*files);
  153.  
  154.                                     /* now print the object */
  155.                                     rc = DoMethodA(obj,(Msg) &gd->gd_PrintMsg);
  156.  
  157.                                     /* notify the user, that the printing is done or has been canceled */
  158.                                     NotifyUser(gd,PRERR_NOERR + rc);
  159.                                 } else
  160.                                 {
  161.                                     /* object doesn't support printing */
  162.                                     NotifyUser(gd,MSG_NO_PRINTING_SUPPORTED,(LONG) *files);
  163.                                 }
  164.                                 /* delete this object */
  165.                                 DisposeDTObject(obj);
  166.                             } else if(IoErr() != 0)
  167.                             {
  168.                                 NotifyUser(gd,IoErr(),*files);
  169.                                 SetIoErr(0);
  170.                             }
  171.  
  172.                             files++;
  173.                         }
  174.  
  175.                         if(gd->gd_GInfo.gi_Screen)
  176.                         {
  177.                             if(gd->gd_GInfo.gi_DrInfo)
  178.                                 FreeScreenDrawInfo(gd->gd_GInfo.gi_Screen,gd->gd_GInfo.gi_DrInfo);
  179.  
  180.                             UnlockPubScreen(NULL,gd->gd_GInfo.gi_Screen);
  181.                         }
  182.  
  183.                         CloseDevice((struct IORequest *) gd->gd_PrintIO);
  184.                     }
  185.  
  186.                     if(gd->gd_PrintIO)
  187.                         DeleteIORequest(gd->gd_PrintIO);
  188.  
  189.                     if(gd->gd_PrintPort)
  190.                         DeleteMsgPort(gd->gd_PrintPort);
  191.  
  192.                     FreeArgs(args);
  193.                 }
  194.  
  195.                 if(LocaleBase)
  196.                 {
  197.                     if(gd->gd_Catalog)
  198.                         CloseCatalog(gd->gd_Catalog);
  199.                     CloseLibrary(LocaleBase);
  200.                 }
  201.  
  202.                 CloseLibrary(IntuitionBase);
  203.                 CloseLibrary(UtilityBase);
  204.                 CloseLibrary(IconBase);
  205.                 CloseLibrary(DataTypesBase);
  206.             }
  207.  
  208.             /* an error occured, so get the object, which caused that error */
  209.             if(gd->gd_ErrorObject != NULL)
  210.                 errobj = gd->gd_ErrorObject;
  211.         }
  212.  
  213.         if(DOSBase)
  214.         {
  215.             if(IoErr())
  216.             {
  217.                 PrintFault(IoErr(),errobj);
  218.  
  219.                 retval = RETURN_ERROR;
  220.             }
  221.             CloseLibrary(DOSBase);
  222.         }
  223.  
  224.         Free(gd);
  225.     }
  226.  
  227.     return(retval);
  228. }
  229. /*FE*/
  230.  
  231. /*FS*/ static STRPTR GetString(struct GlobalData *gd,LONG id)
  232. {
  233.     LONG   *l;
  234.     UWORD  *w;
  235.     STRPTR  builtIn;
  236.  
  237.     l = (LONG *)CatCompBlock;
  238.  
  239.     while (*l != id)
  240.     {
  241.         w = (UWORD *)((ULONG)l + 4);
  242.         l = (LONG *)((ULONG)l + (ULONG)*w + 6);
  243.     }
  244.     builtIn = (STRPTR)((ULONG)l + 6);
  245.  
  246.     if(LocaleBase)
  247.         return(GetCatalogStr((struct Catalog *) gd->gd_Catalog,id,builtIn));
  248.  
  249.     return(builtIn);
  250.  
  251. }
  252. /*FE*/
  253. /*FS*/ static void SetError(struct GlobalData *gd,LONG err,STRPTR errobj)
  254. {
  255.     SetIoErr(err);
  256.     gd->gd_ErrorObject = errobj;
  257. }
  258. /*FE*/
  259. /*FS*/ static void NotifyUser(struct GlobalData *gd,LONG msgid,...)
  260. {
  261.     if(!gd->gd_Para.td_Quiet)
  262.     {
  263.         va_list ap;
  264.         STRPTR msg;
  265.  
  266.         va_start(ap,msgid);
  267.  
  268.         msg = gd->gd_Buffer;
  269.         if(msgid >= 2000 && msgid < 3000)
  270.             sprintf(gd->gd_Buffer,"%s: %s",multiprint,GetDTString(msgid));
  271.         else if(msgid < 999)
  272.             Fault(msgid,multiprint,msg,BUFFER_SIZE-1);
  273.         else
  274.             msg = GetString(gd,msgid);
  275.  
  276.         VPrintf(msg,ap);
  277.         Flush(Output());
  278.  
  279.         va_end(ap);
  280.     }
  281. }
  282. /*FE*/
  283.  
  284. /*FS*/ static BOOL FindMethod(Object *obj,ULONG method)
  285. {
  286.     ULONG *methods;
  287.     BOOL retval = FALSE;
  288.  
  289.     if(DoMethod(obj,OM_GET,DTA_Methods,&methods))
  290.     {
  291.         while(*methods != ~0 && *methods != method)
  292.             methods++;
  293.  
  294.         retval = (*methods == method);
  295.     }
  296.  
  297.     return(retval);
  298. }
  299. /*FE*/
  300.  
  301.